﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel.Data;

public partial class PaymentGateway : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //pass list to that method
            Merchant merchant = new Merchant(_dataObject);
            merchant = SessionWrapper.Merchant;
            this.chargingParams = new Dictionary<string, string>();
            var currentTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            chargingParams.Add("merchant_id", "test_t589"); //Your MID issued by PayZippy.
            chargingParams.Add("buyer_email_address", "kausha87somaiya@gmail.com"); // Email Address
            chargingParams.Add("merchant_transaction_id", merchant.SubscriptionHistories.ElementAt(0).Id.ToString()); //Your Transaction Id
            chargingParams.Add("transaction_type", "SALE"); //This is the default Value.
            chargingParams.Add("transaction_amount", (2).ToString()); //Amount must be in paise. So, 1 Rupee= 100.
            chargingParams.Add("payment_method", "PAYZIPPY"); // CREDIT,DEBIT,EMI,NET
            chargingParams.Add("bank_name", "ICICI"); //Bank Name required in case of EMI/NET.
            chargingParams.Add("emi_months", "0"); // Emi Months in case of EMI.
            chargingParams.Add("currency", "INR"); //INR is default.
            chargingParams.Add("ui_mode", "REDIRECT"); //REDIRECT/IFRAME.
            chargingParams.Add("hash_method", "SHA256"); //MD5, SHA256
            chargingParams.Add("merchant_key_id", "payment"); //This is the default value.
            //address info
            //chargingParams.Add("billing_name",userData.ElementAt(0).Firstname);
            //chargingParams.Add("buyer_phone_no",DiliveryAddress.ElementAt(0).Phoneno);

            //chargingParams.Add("billing_address", DiliveryAddress.ElementAt(0).Address);
            //chargingParams.Add("billing_city", DiliveryAddress.ElementAt(0).CityName);
            //chargingParams.Add("billing_state", DiliveryAddress.ElementAt(0).StateName);
            //chargingParams.Add("billing_country", DiliveryAddress.ElementAt(0).CountryName);
            //chargingParams.Add("buyer_unique_id", ordrNo);
            //chargingParams.Add("billing_zip", DiliveryAddress.ElementAt(0).PostalCode);

            ////shipping address
            //chargingParams.Add("shipping_address", DiliveryAddress.ElementAt(0).Address);
            //chargingParams.Add("shipping_city", DiliveryAddress.ElementAt(0).CityName);
            //chargingParams.Add("shipping_state", DiliveryAddress.ElementAt(0).StateName);
            //chargingParams.Add("shipping_country", DiliveryAddress.ElementAt(0).CountryName);
            //chargingParams.Add("shipping_zip", DiliveryAddress.ElementAt(0).PostalCode);

            //ProductInfo
            string Item = null;
            //for (int i = 0; i < list.Count; i++)
            //{
            //    if (i == 0)
            //        Item += list.ElementAt(i).ProductName;
            //    else
            //        Item += "," + list.ElementAt(i).ProductName;
            //}
            chargingParams.Add("item_vertical", Item);
            chargingParams.Add("timegmt", currentTime.ToString());
            chargingParams.Add("callback_url", "http://www.fonebuddy.in/transactioncompleted.aspx");
            chargingParams.Add("hash", GenHash(chargingParams));

        }
    }
    //protected PayZippy()
    //{
    //    //this.Init += Charging_Init;
    //    //Main();
    //}
  //  private static string secretKey = "8ec6fb441b52f026d5a58a0545dfb01d530debcba801d6a153745a471bf83543";
    private static string secretKey = "ae428a88438d3e4379415bbaa0fb674e0e4f733c2b71bf3313492d34006ac8a7";
    private static string generateSHA256(String input)
    {
        SHA256Managed crypt = new SHA256Managed();
        string hash = String.Empty;
        byte[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(input), 0, Encoding.ASCII.GetByteCount(input));
        foreach (byte bit in crypto)
        {
            hash += bit.ToString("x2");
        }
        return hash;
    }
    static string GenHash(Dictionary<string, string> chargingParams)
    {
        // Acquire keys and sort them.
        List<string> list = new List<string>(chargingParams.Keys);
        list.Sort();
        StringBuilder stringForHash = new StringBuilder();
        // Loop through keys.
        foreach (var key in list)
        {
            stringForHash.Append(chargingParams[key] + '|');
        }
        stringForHash.Append(secretKey);
        return generateSHA256(stringForHash.ToString());
    }
    public Dictionary<string, string> chargingParams;
    public Dictionary<string, string> Url;
    private void Charging_Init(object sender, EventArgs e)
    {
        // this.EnableViewState = false;
    }
}